package com.GatherFour;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class GatherFourActivity extends Activity {
    /** Called when the activity is first created. */

	
	
	
	
	
	
	
	private Button createHost;
	private Button scanHost;
	private Player user;
	private EditText edName;
	private boolean isWifiP2pEnabled = false;
	private BroadcastReceiver receiver = null;
	private final IntentFilter intentFilter = new IntentFilter();
	
    public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
        this.isWifiP2pEnabled = isWifiP2pEnabled;
        if(isWifiP2pEnabled == false){
    	Toast.makeText(GatherFourActivity.this,"WiFi-Direct is disabled, please enable it in system settings.",
                Toast.LENGTH_LONG).show();
        }else{
        	Toast.makeText(GatherFourActivity.this,"WiFi-Direct Available.",
                    Toast.LENGTH_SHORT).show();
        }
        
    }
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.main);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        
        setButtonView();
        
    }
    
    /** Initialize and link buttons to respective IDs. */
    public void setButtonView(){
    	createHost = (Button)this.findViewById(R.id.createHostID);
    	scanHost = (Button)this.findViewById(R.id.scanHostID);
    	edName = (EditText)this.findViewById(R.id.playerName);
    //	chName = (Button)this.findViewById(R.id.changeNameID);
    	user = new Player("Player");
    	/*
    	chName.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				user.name = edName.getText().toString();
			}
		});
    	*/
    	createHost.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(isWifiP2pEnabled == true){
					user.name = edName.getText().toString();
					Intent myIntent = new Intent(v.getContext(), WiFiDirectHost.class);
					myIntent.putExtras(setBundleInfo());
					startActivity(myIntent);
				} else{
					Toast.makeText(GatherFourActivity.this,"WiFi-Direct is disabled, please enable it in system settings.",
			                Toast.LENGTH_LONG).show();
				}
				
			}   		
    	});
    	
    	scanHost.setOnClickListener(new View.OnClickListener() {
    		
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(isWifiP2pEnabled == true){
					user.name = edName.getText().toString();
					Intent myIntent = new Intent(v.getContext(), WiFiDirectScanner.class);
					myIntent.putExtras(setBundleInfo());
					startActivity(myIntent);
				}else{
					Toast.makeText(GatherFourActivity.this,"WiFi-Direct is disabled, please enable it in system settings.",
			                Toast.LENGTH_LONG).show();
				}
			}   		
    	});
    }
    
    @Override
    public void onResume() {
        super.onResume();
        receiver = new WiFiDirectBroadcastReceiver(null, null, this);
        registerReceiver(receiver, intentFilter);
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }
    
    public Bundle setBundleInfo(){
    	Bundle bundle = new Bundle();
    	bundle.putString("playerName", user.name);
		return bundle;
    	
    }
    
    
}